﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using StuManModels;
using StuManDAL;


namespace StudentManageWinForm
{
    public partial class FrmAddStudent : Form
    {
        public FrmAddStudent()
        {
            InitializeComponent();
            Init();
        }
        private StudentService _service = null;
        private ClassService _classService = null;
        #region 业务逻辑
        private void Init()
        {
            _classService = new ClassService();
            cmbClass.DataSource = _classService.GetAllClass(out _);
            cmbClass.DisplayMember = "ClassName";
            cmbClass.ValueMember = "ClassId";
            cmbClass.SelectedIndex = -1;
            _service = new StudentService();
            if (Program.TemporarySaveStudent!=null)
            {
                ModelToUi(Program.TemporarySaveStudent);
            }
        }
        private int BrithdayToAge(DateTime brithday)
        {
            if (DateTime.Now.DayOfYear-brithday.DayOfYear>=0)
            {
                return DateTime.Now.Year - brithday.Year;
            }
            else
            {
                return DateTime.Now.Year - brithday.Year - 1;
            }
        }
        private void ModelToUi(Student student)
        {
            txtbName.Text = student.StuName;
            rbtnMale.Checked = student.IsMale;
            cmbClass.SelectedItem = student.StuClassId;
            txtbPhoneNumber.Text = student.PhoneNumber;
            txtbCardNo.Text = student.CardNo;
            dtpBrithday.Value = student.Birthday;
            txtbIDNumber.Text = student.IDNumber;
            txtbAddress.Text = student.Address;
        }
        private Student UiToModel(out string info)
        {
            if (string.IsNullOrEmpty(txtbName.Text.Trim()))
            {
                info = "姓名不能为空";
                return null;
            }
            if (cmbClass.SelectedIndex==-1)
            {
                info = "班级不能为空";
                return null;
            }
            string phoneNumber = txtbPhoneNumber.Text.Trim();
            if (!string.IsNullOrEmpty(phoneNumber)&&phoneNumber.Length!=11)
            {
                info = "手机号码格式错误";
                return null;
            }
            if (string.IsNullOrEmpty(txtbCardNo.Text.Trim()))
            {
                info = "卡号不能为空";
                return null;
            }
            string idNumber = txtbIDNumber.Text.Trim();
            if (string.IsNullOrEmpty(idNumber))
            {
                info = "身份证号不能为空";
                return null;
            }
            if (!StuManDAL.Common.DataValidate.Instance.IsIdentityCard(idNumber))
            {
                info = "身份证格式错误";
                return null;
            }
            bool idNumberResult = _service.IsIDNumberExist(idNumber, out string idNumberInfo);
            if (!idNumberResult)
            {
                info = idNumberInfo;
                return null;
            }
            string brithday = dtpBrithday.Value.ToString("yyyy-MM-dd");
            if (!idNumber.Contains(brithday.Replace("-", string.Empty)))
            {
                info = "出生日期与身份证号不匹配";
                return null;
            }
            info = "转换成功";
            return new Student()
            {
                StuName = txtbName.Text.Trim(),
                IsMale = rbtnMale.Checked,
                StuClassId = Convert.ToInt32(cmbClass.SelectedValue),
                CardNo = txtbCardNo.Text.Trim(),
                IDNumber = idNumber,
                Birthday = dtpBrithday.Value,
                PhoneNumber = phoneNumber,
                Address = txtbAddress.Text.Trim(),
                Age = BrithdayToAge(dtpBrithday.Value)
            };
        }
        private void AddStudent()
        {
            Student student = UiToModel(out string info);
            lbInfo.Text = info;
            if (student == null) return;
            _service.AddStudent(student, out string addInfo);
            lbInfo.Text = addInfo;
        }
        private void SaveStudent()
        {
            Student student = UiToModel(out string info);
            lbInfo.Text = info;
            if (student!=null)
            {
                Program.TemporarySaveStudent = student;
            }
        }
        #endregion
        #region 事件处理方法
        private void btnSubmit_Click(object sender, EventArgs e)
        {
            AddStudent();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            SaveStudent();
        }
        #endregion
    }
}
